package com.zkingsoft.actions.visitor;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.Nav;
import com.zkingsoft.model.ShowBox;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.model.ShowElementCate;
import com.zkingsoft.model.ShowPoint;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.FunctionService;
import com.zkingsoft.services.MessageService;
import com.zkingsoft.services.NavService;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.ShowBoxService;
import com.zkingsoft.services.ShowElementCateService;
import com.zkingsoft.services.ShowElementService;
import com.zkingsoft.services.ShowPointService;
import com.zkingsoft.services.UsersService;

/**
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2015年9月29日10:31
 * @description 访问数据的action,
 */
@RequestMapping(value = "/vdata")
@Controller
public class VisitorDataController extends BaseController {

	@Resource
	private UsersService usersService;
	@Resource
	private FunctionService functionService;
	@Resource
	private RolePwoerService rolePwoerService;

	@Resource
	private NavService navService;
	@Resource
	private ShowPointService showPointService;

	@Resource
	private ShowBoxService showBoxService;

	@Resource
	private ShowElementService showElementService;

	@Resource
	private ShowElementCateService showElementCateService;
	@Resource
	private MessageService messageService;

	ShowElement showElementQuery = new ShowElement();
	ShowElementCate showElementCateQuery = new ShowElementCate();

	ShowBox showboxQuery = new ShowBox();
	ShowPoint showPointQuery = new ShowPoint();

	/**
	 * 获取session中存储的一级 navList
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/firstNav/{companyId}")
	public @ResponseBody AjaxResult firstNav(@PathVariable("companyId") Long companyId) {
		if (getServletContext().getAttribute("navList") == null) {
			Nav navQuery = new Nav();
			navQuery.setGrade(1);
			navQuery.setCompanyId(companyId);
			List<Nav> navList = navService.findByModel(navQuery);
			getServletContext().setAttribute("navList", navList);
			getServletContext().setAttribute("childNav", new HashMap<Long, List<Nav>>());
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, (List<?>) getServletContext().getAttribute("navList"),
				0, 0);
	}

	/**
	 * 根据父级id获取子集nav的集合，在session中做一个缓存
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/childNav/{parentId}")
	public @ResponseBody AjaxResult getChildNav(@PathVariable("parentId") Long parentId) {
		HashMap<Long, List<Nav>> childNav = (HashMap<Long, List<Nav>>) getServletContext().getAttribute("childNav");
		if (childNav.get(parentId) == null) {
			Nav navQuery = new Nav();
			navQuery.setParentId(parentId);
			List<Nav> navList = navService.findByModel(navQuery);
			childNav.put(parentId, navList);
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, childNav.get(parentId), 0, 0);
	}

	/**
	 * 根据公司id和展柜名称获取展位 
	 */
	@RequestMapping(value = "/showPoints")
	public @ResponseBody AjaxResult showPoints(String name, Long companyId) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, showPointService.findByBoxName(name, companyId), 0, 0);
	}

	
	
	/**
	 * 根据公司id和展位名称名称获取展位
	 */
	@RequestMapping(value = "/showPoint")
	public @ResponseBody AjaxResult showPoint(String name, Long companyId) {
		showPointQuery=new ShowPoint();
		showPointQuery.setCompanyId(companyId);
		showPointQuery.setText(name);
		List<ShowPoint> pList= showPointService.findByModel(showPointQuery);
		if(pList.size()!=1){
			return new AjaxResult(AjaxResult.STATUS_ERR,null,"未获取到数据 CODE:"+pList.size());
		}else{
			return new AjaxResult(AjaxResult.STATUS_OK, null, null, pList, 0, 0);
		}
	}

	
}